/*
 * ========================================================
 * Copyright(c) 2014 杭州龙骞科技-版权所有
 * ========================================================
 * 本软件由杭州龙骞科技所有, 未经书面许可, 任何单位和个人不得以
 * 任何形式复制代码的部分或全部, 并以任何形式传播。
 * 公司网址
 * http://www.hzdracom.com/
 * ========================================================
 */
package com.shwread.android.qysw.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.TextView;
import com.shwread.android.qysw10000136.R;
import logic.bean.EnterpriseColumn;
import logic.util.Util;

/**
 * @Project： Lqyd_Android
 * @Title：InformationURLActivity
 * @Description：
 * @Author：wayne
 * @Date：2014年9月3日上午4:41:12
 */
public class InformationURLActivity
        extends
        BaseBusinessActivity
        implements
        OnClickListener
{
	
	private EnterpriseColumn info;
	
	private TextView         tvTitle;
	private Button           btnBack;
	private TextView         tvNewsTitle;
	private TextView         tvNewsAuthor;
	private TextView         tvNewsTime;
	private WebView          webNewsContent;
	
	private Handler          handler = new Handler()
	                                 {
		                                 
		                                 @Override
		                                 public void handleMessage(Message msg) {
			                                 super.handleMessage(msg);
		                                 }
		                                 
	                                 };
	
	@Override
	public Handler getUIHandler() {
		return handler;
	}
	
	private Context context;
	
	int index = 1;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.enterprise_news_detail);
		context= this;
		info = (EnterpriseColumn) getIntent().getSerializableExtra("EnterpriseColumn");
		initView();
		setListener();
		System.out.println("资讯详情：" + info.getContent());
		if (null != info)
		{
			initData();
		}
	}
	
	@Override
	protected void onResume() {
		super.onResume();
	}
	
	void initView() {
		tvTitle = (TextView) findViewById(R.id.common_header_name);
		btnBack = (Button) findViewById(R.id.common_header_back);
		btnBack.setVisibility(View.VISIBLE);
		tvNewsTitle = (TextView) findViewById(R.id.enterprise_news_detail_title);
		tvNewsAuthor = (TextView) findViewById(R.id.enterprise_news_detail_author);
		tvNewsTime = (TextView) findViewById(R.id.enterprise_news_detail_time);
		webNewsContent = (WebView) findViewById(R.id.enterprise_news_detail_web);
		tvTitle.setText("资讯内容");
	}
	
	@SuppressLint ("SetJavaScriptEnabled")
	void setListener() {
		btnBack.setOnClickListener(this);
		webNewsContent.getSettings().setDefaultTextEncodingName("UTF-8");
		webNewsContent.getSettings().setJavaScriptEnabled(true);
		webNewsContent.getSettings().setSupportZoom(true);
//		webNewsContent.setWebChromeClient(new WebChromeClient() 
//        {          
//            public void onProgressChanged(WebView view, int progress)   
//            {            
//            	((Activity) context).setTitle("Loading...");       
//            	((Activity) context).setProgress(progress * 100);     
//                if(progress == 100)            
//                	context.setTheme(R.string.app_name);       
//                }      
//            }
//        );      
		webNewsContent.setWebViewClient(new WebViewClient()
		{
			
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				
				webNewsContent.loadUrl(url);
				index++;
				return true;
			}
			
		});
		
		webNewsContent.setOnKeyListener(new OnKeyListener()
		{
			
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				if (event.getAction() == KeyEvent.ACTION_DOWN)
				{
					if (keyCode == KeyEvent.KEYCODE_BACK && webNewsContent.canGoBack())
					{ 
						index--;
						//表示按返回键  
						webNewsContent.goBack(); //后退    
						if(index==1)webNewsContent.loadDataWithBaseURL(null, fomateHtml(info.getContent()), "text/html", "UTF-8", null);
						//webview.goForward();//前进  
						return true; //已处理    
					}
				}
				return false;
			}
		});
	}
	
	void initData() {
		tvNewsTitle.setText(info.getTitle());
		if (Util.isEmpty(info.getSource()))
		{
			tvNewsAuthor.setVisibility(View.GONE);
		}
		else
		{
			tvNewsAuthor.setVisibility(View.VISIBLE);
			tvNewsAuthor.setText(info.getSource());
		}
		tvNewsTime.setText(Util.getTimeByDuration(Util.getStringtoDate(info.getPublicTime()).getTime()));
		
		webNewsContent.loadDataWithBaseURL(null, fomateHtml(info.getContent()), "text/html", "UTF-8", null);
	}
	
	private String fomateHtml(String content) {
		StringBuilder sb = new StringBuilder();
		sb.append("<html>");
		sb.append("<meta http-equiv='Content-Type' content='text/html;charset=UTF-8'>");
		sb.append(content);
		sb.append("</html>");
		return sb.toString();
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.common_header_back:
				this.onBackPressed();
				break;
			
			default:
				break;
		}
		
	}
	
}
